package com.fly.views.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fly.views.entity.JsonResult;
import com.fly.views.service.TokenService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "系统接口")
@RestController
@RequestMapping("/api")
public class ApiController
{
    @Autowired
    HttpSession httpSession;
    
    @Autowired
    TokenService tokenService;
    
    @ApiOperationSupport(order = 10)
    @PostMapping("/login")
    @ApiOperation("登录系统")
    public JsonResult<?> login(String token)
    {
        if (!tokenService.valide(token))
        {
            return JsonResult.error("token empty or valide failed!");
        }
        httpSession.setAttribute("token", token);
        String date = DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        return JsonResult.success(date + " login success!");
    }
    
    @ApiOperationSupport(order = 20)
    @PostMapping("/logout")
    @ApiOperation("退出系统")
    public JsonResult<?> logout()
    {
        httpSession.invalidate();
        String date = DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        return JsonResult.success(date + " logout success!");
    }
}
